/* Table 2 */



#delimit ;
clear all;
local outfile "GWgapr10_indivHLFSb";
set more off;


di _n "$S_DATE $S_TIME";







********************************************************************************;
* preparing data;
********************************************************************************;

use if Q_rest_HLFS==1 using GWgap_pr_HLFS_v4, clear;


* creating variables that mark the combinations of year with various other categorical variables;

egen occ3d_yr = group(occ3d_HLFS year);
label var occ3d_yr "Groups defined by 3d occupation and financial year";
if "`samp'"=="on" replace occ3d_yr = floor(occ3d_yr/20);

egen eth_yr = group(eth_gp year);
label var eth_yr "Groups defined by ethnicity combination (15 categories) and financial year";

egen hqual_yr = group(hqual year);
label var hqual_yr "Groups defined by highest qualification and financial year";

egen rc_yr = group(rc_HLFS year);
label var rc_yr "Groups defined by Regional Council and financial year";

egen hhcomp_yr = group(hhcomp_HLFS year);
label var hhcomp_yr "Groups defined by family composition and financial year";

egen numkid_yr = group(numkid_u18_cat year);
label var numkid_yr "Groups defined by number of kids parented and financial year";

egen ind4_yr = group(ind4 year);
label var ind4_yr "Groups defined by 4-digit ANZSIC industry and financial year";
if "`samp'"=="on" replace ind4_yr = floor(ind4_yr/20);


rename ind4 ind4u;

foreach var in max_fte_employee_av hrs_main_HLFS {;
	gen ln`var' = ln(`var');
	local lab: var label `var';
	label var ln`var' "`lab' (ln)";
};

* year interactions with continuous variables;

forvalues year = 2008/2016 {;

	gen y`year'_hrs = (year==`year')*lnhrs_main_HLFS;

	gen y`year'_age = (year==`year')*age;
	gen y`year'_age2 = (year==`year')*age2;
	gen y`year'_KL_rat = (year==`year')*KL_rat;
	gen y`year'_lnL_hc = (year==`year')*lnL_hc;
		
};







********************************************************************************;
* creating macros;
********************************************************************************;


local dvar lnIDI_earnhp; 
local dvarl: var label `dvar';

local indicators `"
	"Year FE = *year*"
	"Ethnicity combination FE (15 categories) = *eth_gp*"
	"Regional Council FE = *rc_HLFS*"
	"Occupation FE (3-digit ANZSCO 2006) = *occ3d_HLFS*"
	"Industry FE (4-digit ANZSIC) = *ind4u*"
	"Hours worked (ln) by year = *y20*_hrs*"
	"Age quadratic by year = y20*age y20*age2"
	"Highest qualification by year FE = *hqual_yr*"
	"Household composition by year FE = *hhcomp_yr*"
	"Number of children by year FE = *numkid_yr*"
	"Ethnicity combination by year FE = *eth_yr*"
	"Regional Council by year FE = *rc_yr*"
	"Occupation by year FE = *occ3d_yr*"
	"'; 


	
*** regression controls;

local c1 "female lnhrs_main_HLFS age age2 i.year";
local c2 "`c1' i.hqual i.hhcomp_HLFS i.numkid_u18_cat i.eth_gp i.rc_HLFS "; 
local c3 "`c2' i.occ3d_HLFS"; 
local c4 "`c3' i.ind4u";
local c5 "`c4' y20*_hrs y20*age y20*age2 i.hqual_yr i.hhcomp_yr i.numkid_yr i.eth_yr i.rc_yr i.occ3d_yr "; * absorb ind4_yr;
local c6 "`c4' wkd_hpp_1ya wkd_hpp_2ya";
local c7 "`c6' lnL_hc KL_rat val_ad_pw";
local c8 "`c7' hp_ffe_m";









********************************************************************************;
* running regs;
********************************************************************************;

capture erase "`outfile'.txt";


foreach sample in Q_rest {; * looping over sample to be included;

	if "`sample'"=="all" {;
		local samplel "All available observations";
		local samplec "`dvar'<.";
	};
	if "`sample'"=="consis" {;
		local samplel "Consistent sample across all specifications of all available observations";
		local samplec "nonmsg==1";
	};
	if "`sample'"=="Q_rest" {;
		local samplel "Restricted consistent sample used for the rest of the analysis";
		local samplec "Q_rest==1";
	};

	eststo clear;
	local headers "";

	forvalues i = 1/8 {;
	
		if `i'!=5 {;
			reg `dvar' `c`i'' if `samplec', vce(cluster snz_uid);
			*rndobs N, seed(0871525);
			eststo;
			local headers `" `headers' "(`i')" "';
		};
		if `i'==5 {;
			areg `dvar' `c`i'' if `samplec', vce(cluster snz_uid)  absorb(ind4_yr);
			*rndobs N, seed(0871525);
			eststo;
			local headers `" `headers' "ind4_yr FE" "';
		};
	};

	
	
	estout * using "`outfile'.txt", append
		style(tab) 
		c(b(star fmt(%9.3f)) se(par))
		legend label collabels(, none)
		dropped ("dropped")
		drop(_cons )
		mlabels(`headers')
		varl(`labels')
		indicate(`indicators')
		order(`')
		starlevels(* 0.05 ** 0.01)
		stats(r2 N, 
			fmt(%9.3f %9.0fc ) 
		labels("R-Squared" Observations ))
		prehead("`=char(13)'`=char(13)'
		`=char(13)'*`sample'*****************************************************")
		varwidth(67) modelwidth(11) delimiter("");
	
};

